package com.healthcloud.service.impl;

import cn.jpush.api.bean.TestDateEntity;
import com.healthcloud.dao.SavingSampleInfoDetailDao;
import com.healthcloud.entity.SavingSampleInfoDetailEntity;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.healthcloud.dao.SavingSampleInfoDao;
import com.healthcloud.entity.SavingSampleInfoEntity;
import com.healthcloud.service.SavingSampleInfoService;
import com.healthcloud.utils.PageParam;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


@Service("savingSampleInfoService")
@Transactional
public class SavingSampleInfoServiceImpl implements SavingSampleInfoService {

	@Autowired
	private SavingSampleInfoDao savingSampleInfoDao;

	@Autowired
	private SavingSampleInfoDetailDao savingSampleInfoDetailDao;
	
	@Override
	public SavingSampleInfoEntity queryObject(Long id){
		return savingSampleInfoDao.queryObject(id);
	}
	
	@Override
	public List<SavingSampleInfoEntity> queryList(SavingSampleInfoEntity bean, PageParam pageParam){
		return savingSampleInfoDao.queryList(bean, pageParam);
	}
	
	@Override
	public int queryTotal(SavingSampleInfoEntity bean){
		return savingSampleInfoDao.queryTotal(bean);
	}
	
	@Override
	public void save(SavingSampleInfoEntity savingSampleInfo){
		savingSampleInfoDao.save(savingSampleInfo);
	}


	@Override
	public void saveavingSampleInfo(TestDateEntity testDateEntity) {

		SavingSampleInfoEntity bean = new SavingSampleInfoEntity();
		bean.setAppCode(testDateEntity.getMKMedicalinstrumentId());
		bean.setCreatedTime(new Date());
		savingSampleInfoDao.save(bean);

		SavingSampleInfoDetailEntity entity = new SavingSampleInfoDetailEntity();
		entity.setSsiId(bean.getId());
		entity.setAppCode(testDateEntity.getMKMedicalinstrumentId());
		entity.setTestName(testDateEntity.getTestItemNameen());
		entity.setTestFullName(testDateEntity.getTestItemName());
		entity.setTestResult(testDateEntity.getTestResult());
		entity.setCreatedTime(new Date());

		String low = StringUtils.isBlank(testDateEntity.getConsultLow()) ? "0" : testDateEntity.getConsultLow();
		String high = StringUtils.isBlank(testDateEntity.getConsultHigh())?"0":testDateEntity.getConsultHigh();
		entity.setTestRange(low + "-" + high);
		String raw = "";
		if("0".equals(testDateEntity.getConsultResult())){
			raw = "↓";
		}

		if("9".equals(testDateEntity.getConsultResult())){
			raw = "↑";
		}
		entity.setTestState(raw);
		entity.setTestUnit(testDateEntity.getValueUnit());
		entity.setTestTime(testDateEntity.getTestTime());
		entity.setCreatedTime(new Date());
		savingSampleInfoDetailDao.save(entity);


	}

	@Override
	public void update(SavingSampleInfoEntity savingSampleInfo){
		savingSampleInfoDao.update(savingSampleInfo);
	}
	
	@Override
	public void delete(Long id){
		savingSampleInfoDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		savingSampleInfoDao.deleteBatch(ids);
	}
	
}
